package com.yuke.cloud.service.cmc.controller.frontend;

import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.cmc.dto.CmsCouponUserDto;
import com.yuke.cloud.service.cmc.entity.CmsCoupon;
import com.yuke.cloud.service.cmc.entity.CmsCouponReceive;
import com.yuke.cloud.service.cmc.service.CmsCouponReceiveService;
import com.yuke.cloud.service.cmc.service.CmsCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/*
 * <p>Company: Copyright (c) 2018. yuke All Rights Reserved. </br> </p>
 * <p>Title：yukemall </br> </p>
 * <p>com.yuke.cloud.service.cmc.controller.frontend Description TODO </br> </p>
 * @Author wg
 * @Date   2019/7/23 10:36
 */
@Api(value = "WEB - MallCmsCouponController", tags = "APP版本相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/mall/cms/coupon")
public class MallCmsCouponController extends BaseController {
    @Autowired
    private CmsCouponService cmsCouponService;

    @Resource
    private CmsCouponReceiveService cmsCouponReceiveService;



    //本處代碼在cmsCouponReceiveController.java也有，為了調試方便，規避權限及token保留
    //通过shop_id查找领券信息
    @GetMapping("/shopId/{shopId}")
    @ApiOperation(httpMethod = "GET", value = "获取所有可用记录")
    public Wrapper<List<CmsCouponUserDto>> getUserListByShopId(@PathVariable Long shopId) {

        CmsCouponUserDto cmsCouponUserDto = new CmsCouponUserDto();
        cmsCouponUserDto.setReceiveType((byte)1);
        cmsCouponUserDto.setReceiveId(shopId);


        List<CmsCouponUserDto> list = cmsCouponReceiveService.getCouponUserList(cmsCouponUserDto);

        return WrapMapper.ok(list);
    }

    @PostMapping("/getCoupon")
    @ApiOperation(httpMethod = "POST", value = "新增一条领取优惠券记录")
    @Transactional
    public Wrapper add(@ApiParam(name = "cmsCouponReceive", value = "该数据记录的POJO") @RequestBody CmsCouponReceive cmsCouponReceive) {

        if (cmsCouponReceive.getCouponId()!=null) {
            CmsCoupon cmsCoupon = cmsCouponService.findById(cmsCouponReceive.getCouponId());
            //检查优惠券剩余数量，领券时间
            if (cmsCoupon!=null){
                //检查是否已经领取超过限额的优惠券数量
                List<CmsCouponReceive> couponUser = cmsCouponReceiveService.select(cmsCouponReceive);
                if (couponUser.size()>cmsCoupon.getTag()){
                    return WrapMapper.error("该优惠券只能领取"+cmsCoupon.getTag()+"张！");
                }
                //检查时间限制
                Date nowtime = new Date();
                if(cmsCoupon.getSendStartDate().getTime() >= nowtime.getTime()){
                    return WrapMapper.error("该优惠券还未到领取时间！");
                }
                if(cmsCoupon.getSendEndDate().getTime() <= nowtime.getTime()){
                    return WrapMapper.error("该优惠券已过期！");
                }
                if (cmsCoupon.getNumber()-cmsCoupon.getNowNum()>1){
                    //领取1张
                    cmsCoupon.setNowNum(cmsCoupon.getNowNum()+1);
                    cmsCouponService.update(cmsCoupon);
                    cmsCouponReceiveService.save(cmsCouponReceive);

                }else{
                    return WrapMapper.error("该优惠券已经没有了！");
                }
            }
        }else{
            return WrapMapper.error("程序错误。优惠券ID为空！");
        }
        return WrapMapper.ok();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除cms_coupon_receive表中的一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        cmsCouponReceiveService.deleteById(id);
        return WrapMapper.ok();
    }
}
